package com.ycb.bookserver.service.impl;

import com.book.constant.MessageConstant;
import com.book.constant.BookStatusConstant;
import com.book.context.BaseContext;
import com.book.exception.BookStatusException;
import com.book.exception.ExtendMaxException;
import com.book.result.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ycb.book.dto.BookDTO;
import com.ycb.book.dto.BookPageQueryDTO;
import com.ycb.book.dto.CategoryDTO;
import com.ycb.book.entity.Book;
import com.ycb.book.entity.User;
import com.ycb.book.vo.BookVO;
import com.ycb.book.vo.BookVO1;
import com.ycb.bookserver.mapper.CategoryMapper;
import com.ycb.bookserver.mapper.UserMapper;
import com.ycb.bookserver.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ycb.bookserver.Mapper.BookMapper;


import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class BookServiceImpl implements BookService {
    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public void addBook(BookDTO bookDTO) {
        Book book=new Book();
        System.out.println("当前线程id:"+Thread.currentThread().getId()+"3");
        BeanUtils.copyProperties(bookDTO,book);
        book.setStatus(BookStatusConstant.ENABLE);

        log.info("当前线程:{}", BaseContext.getCurrentId());
        User user= userMapper.getUserById(BaseContext.getCurrentId());
        log.info("拿取用户信息:{}",user.getId());
        book.setUserId(user.getId());


        bookMapper.add(book);
        if (user.getMySelling().size()<20) {
            user.getMySelling().add(book);
        }else {
            throw new ExtendMaxException(MessageConstant.EXTEND_MAX_SELLING);
        }

        BaseContext.removeCurrentId();
    }

    @Override
    public BookVO1 getById(Long id) {
        Book book=bookMapper.getById(id);
        Long id1=book.getUserId();
        User user=userMapper.getUserById(id1);
        BookVO1 bookVO=new BookVO1();
        BeanUtils.copyProperties(book,bookVO);
        bookVO.setPublisher(user.getUserName());
        bookVO.setPublisherHead(user.getHead());
        return bookVO;
    }

    @Override
    public void modify(BookDTO bookDTO) {
        Book book=new Book();
        BeanUtils.copyProperties(bookDTO,book);

        bookMapper.modify(book);
    }

    /**
     * 修改书籍出售状态
     * @param status
     * @param id
     */
    @Override
    public void sold(Integer status, Long id) {
        Book book=Book.builder()
                .id(id)
                .status(status)
                .build();
        //判断书籍是否被禁售
        if (book.getStatus()==BookStatusConstant.DISABLE) {
            throw new BookStatusException(MessageConstant.BOOK_DISABLE);
        }

        bookMapper.modify(book);
    }

    /**
     * 删除书籍
     * @param id
     */
    @Override
    public void delete(Long id) {
        Book book=bookMapper.getById(id);
        if (!book.getStatus().equals(BookStatusConstant.ENABLE)){
            bookMapper.delete(id);
        }else {
            throw new BookStatusException(MessageConstant.BOOK_ON_SALE);
        }
    }

    /**
     * 书籍分页查询
     * @param bookPageQueryDTO
     * @return
     */
    @Override
    public PageResult search(BookPageQueryDTO bookPageQueryDTO) {
        PageHelper.startPage(bookPageQueryDTO.getPage(),bookPageQueryDTO.getPageSize());
        Page<BookVO> bookVOS=bookMapper.search(bookPageQueryDTO);
        Long total=bookVOS.getTotal();
        List<BookVO> bookVOS1=bookVOS.getResult();
        PageResult pageResult=new PageResult(total,bookVOS1);
        return pageResult;
    }

    /**
     * 批量删除书籍
     * @param ids
     */
    @Override
    public void deleteBatch(List<Long> ids) {
        //获取书籍
        List<Book> books=new ArrayList<>();
        for (Long id : ids) {
            Book book=bookMapper.getById(id);
            books.add(book);
        }
        //判断是否有书籍在出售中
        for (Book book : books) {
            if(!book.getStatus().equals(BookStatusConstant.ENABLE)){
                //有书籍在出售中，不能删除
                throw new BookStatusException(MessageConstant.BOOK_ON_SALE);
            }
        }
      bookMapper.deleteByIds(ids);
    }

    /**
     * 查询所有书籍
     * @return
     */
    @Override
    public List<BookVO> allBook() {
        List<Book> books=bookMapper.allBook();
        List<BookVO> bookVOS=new ArrayList<>();
        for (Book book : books) {
            BookVO bookVO=new BookVO();
            BeanUtils.copyProperties(book,bookVO);
            bookVOS.add(bookVO);
        }
        return bookVOS;
    }

    /**
     * 根据分类查询书籍
     */
    @Override
    public List<BookVO> BookCategory(CategoryDTO categoryDTO) {
        List<Book> books=bookMapper.allBook();
        List<BookVO> bookVOS=new ArrayList<>();
        for (Book book : books) {
            if(Objects.equals(book.getCategory(), categoryDTO.getId())){
                BookVO bookVO=new BookVO();
                BeanUtils.copyProperties(book,bookVO);
                bookVOS.add(bookVO);
            }
        }
        return bookVOS;
    }

    /**
     * 部分名称查询书籍
     * @param partName
     * @return
     */
    @Override
    public List<BookVO> searchByPartName(String partName) {
        List<Book> bookS=bookMapper.searchByPartName(partName);
        List<BookVO> bookVOS=new ArrayList<>();
        for (Book book : bookS) {
            BookVO bookVO=new BookVO();
            BeanUtils.copyProperties(book,bookVO);
            bookVOS.add(bookVO);
        }
     if (bookVOS==null || bookVOS.isEmpty()){
         throw new BookStatusException(MessageConstant.ORDER_BOOK_NOT_FOUND);
     }
        return bookVOS;
    }

    /**
     * 管理员修改书籍启用禁用
     * @param status
     * @param id
     */
    @Override
    public void soldAdmin(Integer status, Long id) {
        Book book=Book.builder()
                .id(id)
                .status(status)
                .build();
        bookMapper.modify(book);
    }
}
